import { login } from '@/api/authentication'
import { getToken, setToken } from '@/utils/auth'

const state = {
  token: getToken() || '', // 令牌
  userInfo: {} // 用户信息
}

const getters = {

}

const mutations = {
  // 设置token
  setStateToken(state, token) {
    state.token = token
  },

  // 设置userInfo
  setStateUserInfo(state, userInfo) {
    state.userInfo = userInfo
  }
}

const actions = {
  async getUserInfo(context, params) {
    const result = await login(params)
    setToken(result.data.data.token.token)
    context.commit('setStateToken', result.data.data.token.token)
    context.commit('setStateUserInfo', result.data.data.user)
  }
}

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
